<template>
  <div class="app-container">
    <header class="app-header">
      <h1>你的AI助理</h1>
    </header>
    <div class="chat-container">
      <div class="chat-messages" ref="messagesContainer">
        <div v-for="(message, index) in messages" :key="index" 
             :class="['message-wrapper', message.role === 'user' ? 'user-message-wrapper' : 'assistant-message-wrapper']">
          <div class="avatar">
            <img :src="message.role === 'user' ? '/picture/user/user.png' : '/picture/ai/ai.png'" :alt="message.role">
          </div>
          <div class="message-content">
            <div class="message-bubble">{{ message.content }}</div>
          </div>
        </div>
      </div>
      <div class="chat-input">
        <el-input
          v-model="inputMessage"
          type="textarea"
          :rows="3"
          placeholder="请输入您的问题..."
          @keyup.enter.ctrl="sendMessage"
        />
        <el-button type="primary" @click="sendMessage" :loading="loading">
          发送
        </el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, onMounted, nextTick } from 'vue'
import axios from 'axios'
import { ElMessage } from 'element-plus'

export default {
  name: 'App',
  setup() {
    const messages = ref([])
    const inputMessage = ref('')
    const loading = ref(false)
    const messagesContainer = ref(null)

    const scrollToBottom = async () => {
      await nextTick()
      if (messagesContainer.value) {
        messagesContainer.value.scrollTop = messagesContainer.value.scrollHeight
      }
    }

    const sendMessage = async () => {
      if (!inputMessage.value.trim() || loading.value) return

      const userMessage = { role: 'user', content: inputMessage.value }
      messages.value.push(userMessage)
      loading.value = true

      try {
        const response = await axios.post('http://localhost:5000/api/chat', {
          messages: messages.value
        })

        const assistantMessage = {
          role: 'assistant',
          content: response.data.choices[0].message.content
        }
        messages.value.push(assistantMessage)
      } catch (error) {
        ElMessage.error('发送消息失败，请重试')
      } finally {
        loading.value = false
        inputMessage.value = ''
        scrollToBottom()
      }
    }

    onMounted(() => {
      scrollToBottom()
    })

    return {
      messages,
      inputMessage,
      loading,
      messagesContainer,
      sendMessage
    }
  }
}
</script>

<style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  background-color: #f5f7fa;
}

.app-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.app-header {
  background: linear-gradient(135deg, #1e88e5 0%, #64b5f6 100%);
  color: white;
  padding: 1rem 2rem;
  text-align: center;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.app-header h1 {
  font-size: 1.8rem;
  font-weight: 600;
  margin: 0;
}

.chat-container {
  max-width: 900px;
  margin: 0 auto;
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 20px;
  width: 100%;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  margin-bottom: 20px;
  background-color: #f5f5f5;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
}

.message-wrapper {
  display: flex;
  margin-bottom: 20px;
  align-items: flex-start;
}

.user-message-wrapper {
  flex-direction: row-reverse;
}

.avatar {
  width: 40px;
  height: 40px;
  margin: 0 12px;
  border-radius: 50%;
  overflow: hidden;
  flex-shrink: 0;
}

.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.message-content {
  max-width: 70%;
}

.message-bubble {
  padding: 12px 16px;
  border-radius: 12px;
  background-color: white;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
  line-height: 1.5;
  word-wrap: break-word;
}

.user-message-wrapper .message-bubble {
  background-color: #95EC69;
}

.assistant-message-wrapper .message-bubble {
  background-color: white;
}

.chat-input {
  display: flex;
  gap: 12px;
  padding: 20px;
  background-color: white;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
}

.el-input {
  flex: 1;
}

.el-input__inner {
  border-radius: 8px;
  border: 1px solid #e0e0e0;
  padding: 12px;
  font-size: 14px;
  line-height: 1.5;
  resize: none;
}

.el-input__inner:focus {
  border-color: #1e88e5;
  box-shadow: 0 0 0 2px rgba(30, 136, 229, 0.1);
}

.el-button {
  align-self: flex-end;
  padding: 12px 24px;
  border-radius: 8px;
  font-weight: 500;
  transition: all 0.3s ease;
}

.el-button--primary {
  background-color: #1e88e5;
  border-color: #1e88e5;
}

.el-button--primary:hover {
  background-color: #1976d2;
  border-color: #1976d2;
  transform: translateY(-1px);
}

.el-button--primary:active {
  transform: translateY(0);
}

/* 自定义滚动条样式 */
.chat-messages::-webkit-scrollbar {
  width: 6px;
}

.chat-messages::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 3px;
}

.chat-messages::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 3px;
}

.chat-messages::-webkit-scrollbar-thumb:hover {
  background: #a8a8a8;
}
</style> 